package org.example.mall.admin;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hzq
 * @since 2021-07-23
 */
public class GeneratorCode {


    public static void main(String[] args) {
        String modelPath = "/goods";
        String modelName = ".goods";
        String[] includes = {"spec_params"};
        generator(modelPath, modelName, includes);
    }

    /**
     * @param modelPath 模块路径
     * @param modelName     模块包
     * @param includes          要生成表
     */
    public static void generator(String modelPath, String modelName, String[] includes) {

        /*包路径*/
        String userDirPath = System.getProperty("user.dir");
        String basePackagePath = "/src/main/java/org/example/mall/admin";
        String entityPackagePath = userDirPath + basePackagePath + "/model/entity" + modelPath;
        String controllerPackagePath = userDirPath + basePackagePath + "/controller" + modelPath;
        String servicePackagePath = userDirPath + basePackagePath + "/service" + modelPath;
        String serviceImplPackagePath = userDirPath + basePackagePath + "/service" + modelPath + "/impl";
        String mapperPackagePath = userDirPath + basePackagePath + "/mapper" + modelPath;
        String mapperXmlPackagePath = userDirPath + basePackagePath + "/mapper" + modelPath + "/xml";

        /*包名称*/
        String basePackage = "org.example.mall.admin";
        String entityPackage = basePackage + ".model.entity" + modelName;
        String controllerPackage = basePackage + ".controller" + modelName;
        String servicePackage = basePackage + ".service" + modelName;
        String serviceImplPackage = basePackage + ".service" + modelName + ".impl";
        String mapperPackage = basePackage + ".mapper" + modelName;
        String mapperXmlPackage = basePackage + ".mapper" + modelName + ".xml";

        //1、全局配置
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(true)
                .setAuthor("hzq")
                .setOpen(false)
                .setFileOverride(true)
                .setBaseResultMap(true)
                .setSwagger2(true)
                .setServiceName("%sService")
        ;

        //2、数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUrl("jdbc:mysql://seata-mysql:3306/mall?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai")
                .setUsername("root")
                .setPassword("root");

        //3、策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(includes)
                .setEntityLombokModel(true)
                .setChainModel(true)
                .setRestControllerStyle(true)
                .setVersionFieldName("version")
                .setLogicDeleteFieldName("del")
                .setEntityTableFieldAnnotationEnable(true)
        ;

        //4、包名策略配置 不需要生成的模块 只需注释掉路径
        PackageConfig packageConfig = new PackageConfig();
        Map<String, String> packageInfo = CollectionUtils.newHashMapWithExpectedSize(7);
        packageInfo.put(ConstVal.ENTITY_PATH, entityPackagePath);
        packageInfo.put(ConstVal.CONTROLLER_PATH, controllerPackagePath);
        packageInfo.put(ConstVal.SERVICE_PATH, servicePackagePath);
        packageInfo.put(ConstVal.SERVICE_IMPL_PATH, serviceImplPackagePath);
        packageInfo.put(ConstVal.MAPPER_PATH, mapperPackagePath);
        packageInfo.put(ConstVal.XML_PATH, mapperXmlPackagePath);

        packageConfig
                .setPathInfo(packageInfo)
                .setParent("")
                .setEntity(entityPackage)
                .setController(controllerPackage)
                .setService(servicePackage)
                .setServiceImpl(serviceImplPackage)
                .setMapper(mapperPackage)
                .setXml(mapperXmlPackage);

        //5、配置自定义属性注入
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                //是否使用动态数据源
                map.put("useDS", true);
                //动态数据源名称
                map.put("DSName", "manage_master");
                this.setMap(map);
            }
        };

        //6、整合配置
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
//                .setCfg(injectionConfig)
                .setPackageInfo(packageConfig);

        //7、执行
        autoGenerator.execute();
    }

}
